#!/bin/env python
import Dingo.API as api #Imports the Dingo API
import random

NUMSENSOR = 5 #The amount of sensors

__author__ = 'Dan Goldsmith'

class TheSimulation(api.Simulation):  #Main class Required to run as a sensor module
    def __init__(self, graph):  #Initialise the program
        ether = api.Ether(self) #Setup the Transmission Medium
        sensors = (aSensor(graph,ether,i) for i in xrange(NUMSENSOR)) #Create our sensors
        api.Simulation.__init__(self, sensors, graph) #Initialise the simulation
    def run(self): #Must have to Run the simulation
        super(TheSimulation, self).run() #Run the Simulation


class aSensor(api.Sensor): #Sensor object
    def __init__(self,graph,ether,i): 
        #Setup the Sensor coordinates
        x = random.randrange(0,250)
        y = random.randrange(0,250)
        #Create A List of Tasks to be Preformed
        taskList = [theTask(self)]
        #Initilise the Scheduler for this sensor
        #(<self>,<graph>,<Sensor><(X,Y)coords>,<Sensor Color>,<Scheduler Tasks>,<Ether>
        api.Sensor.__init__(self,graph,"Sensor[%d]" %i, (x,y),api.SensorColor.Red, api.Scheduler(taskList,-1),ether)
        
class theTask(api.Task): #A task the sensors preform
    def __init__(self, sensor):
        # self, priority, repetitions, ref-to-sensor
        api.Task.__init__(self, 0, 0, sensor)
        self.itr = random.randrange(0,10)
        self.shapes = ["circle","square","triangle"]

    def task(self): #The Task itself.
        val = self.itr % 3
        
        theShape = self.shapes[val]
        print theShape
        self.sensor.changeShape(theShape)
        #Your Code for each task goes here....
        self.itr += 1
        sleeptime = int(random.uniform(1,4)) #Pause the sim so we can see what is happening
        api.Sleep(sleeptime)
